/**
 * Created by jimmy on 17/3/24.
 */
import { Base } from '../../utils/base.js'

class My extends Base {
  constructor() {
    super();
  }

  //得到用户信息
  getUserInfo(cb) {

    var that = this;
    wx.login({
      success: function (res) {
        // wx.setStorageSync('token', res.code);
        wx.getUserInfo({
          success: function (res) {
            // console.log(res.userInfo);
            // that.updateUserInfo(res.userInfo)
            // console.log(cb);
            typeof cb == "function" && cb(res.userInfo);

            //将用户昵称 提交到服务器
            if (!that.onPay) {
              that._updateUserInfo(res.userInfo);
            }

          },
          fail: function (res) {
            typeof cb == "function" && cb({
              avatarUrl: '../../imgs/icon/user@default.png',
              nickName: '爱车易得'
            });
          }
        });
      },

    })
  }

  /*更新用户信息到服务器*/
  _updateUserInfo(res) {
    var nickName = res.nickName;
    // console.log(wx.getStorageSync('token'));
    delete res.avatarUrl;  //将昵称去除
    delete res.nickName;  //将昵称去除
    var allParams = {
      url: 'user/wx_info',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      data: { nickname: nickName, extend: JSON.stringify(res) },
      type: 'post',
      sCallback: function (data) {
      }
    };
    this.request(allParams);

  }


  paySub(id, callback) {
    var that = this;
    // console.log(id);
    var param = {
      url: 'pay/pre_order',
      type: 'post',
      data: { id: id },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        // 'token': wx.getStorageSync('token')
      },
      sCallback: function (data) {
        // console.log(data);
        data = data;
        callback && callback(data);
      }
    };
    this.request(param);
  }
  //将用户的昵称和头像更新到user表里
  updateUserInfo(param,callback) {
    this.request({
      url: 'wxInfo',
      type: 'post',
      data: param,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync('token')
      },
      sCallback: function (data) {
        
        callback && callback(data);
      },
      eCallback: function (data) {
      }
    })
    // return res
  }


}



export { My }